//
//  DDCalendarViewController.m
//  ialone
//
//  Created by 陆翀 on 16/1/13.
//  Copyright © 2016年 JasperSong. All rights reserved.
//

#import "DDCalendarViewController.h"
#import "DDCalendarTableViewCell.h"
#import "DDCalendarModel.h"
#import "DDCalendarDetailViewController.h"
@interface DDCalendarViewController () <UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) UITableView *tableview;

@property (nonatomic,strong) NSMutableArray *dataArr;

@property (nonatomic,assign)NSInteger requestStart;

@property (nonatomic,strong)NSString *pageid;

@property (nonatomic,strong)NSString *create_time;
@end

@implementation DDCalendarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _dataArr = [NSMutableArray array];
    _requestStart = 1;
    _pageid = @"0";
    _create_time = @"0";
    self.titleLabel.text = _str1;
    self.titleLabel.font = [UIFont systemFontOfSize:28];
    
    [self.leftButton addTarget:self action:@selector(leftButton:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.leftButton setImage:[UIImage imageNamed:@"item_cancel@2x"] forState:(UIControlStateNormal)];
    self.tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, kScreenWidth, kScreenHeight - 64) style:(UITableViewStylePlain)];
    _tableview.dataSource = self;
    _tableview.delegate  = self;
    _tableview.rowHeight = kScreenHeight-64;
    _tableview.pagingEnabled =YES;
    [self.view addSubview:_tableview];
    self.tableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        _requestStart = 1;
        _create_time = @"0";
        _pageid = @"0";
        [self setUpData];
    }];
    
    self.tableview.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        // 进入刷新状态后会自动调用这个block
        _requestStart += 1 ;
        DDCalendarModel *model = _dataArr[_dataArr.count - 1] ;
        _create_time = model.create_time;
        _pageid = model.pageid;
        [self setUpData];
        // 更改start
        
    }];
    [self hubShow];
    [self setUpData];
}
-(void)setUpData
{
    NSString *str = [_str stringByReplacingOccurrencesOfString:@"p=1" withString:[NSString stringWithFormat:@"p=%ld",_requestStart]];
    str = [str stringByReplacingOccurrencesOfString:@"page_id=0" withString:[NSString stringWithFormat:@"page_id=%@",_pageid]];
    str = [str stringByReplacingOccurrencesOfString:@"create_time=0" withString:[NSString stringWithFormat:@"create_time=%@",_create_time]];
    
    NSURL *url = [NSURL URLWithString:str];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url];
    [request setHTTPMethod:@"get"];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *arr = dic[@"datas"];
        [self hubHide];
        if (_requestStart == 1) {
            [_dataArr removeAllObjects];
        }
        for (NSDictionary *one in arr) {
            DDCalendarModel *model = [[DDCalendarModel alloc]init];
            [model setValuesForKeysWithDictionary:one];
            [self.tableview.mj_header endRefreshing];
            [self.tableview.mj_footer endRefreshing];
            [_dataArr addObject:model];
        }
        [_tableview reloadData];
    }];
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *idenfitier = @"cell";
    DDCalendarTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:idenfitier];
    if (cell == nil) {
        cell = [[DDCalendarTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:idenfitier];
    }
    DDCalendarModel *model = _dataArr[indexPath.row];
    
    cell.model = model;
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DDCalendarDetailViewController *detail = [[DDCalendarDetailViewController alloc]init];
    DDCalendarModel *model = _dataArr[indexPath.row];
    detail.html5 = model.html5;
    detail.pageid = model.pageid;
    
    NSLog(@"%@",model.html5);
    [self.navigationController pushViewController:detail animated:YES];
}
- (void)leftButton:(UIButton *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
